package com.simen.connectandroid.command;

import com.simen.connectandroid.command.utils.CommandCreator;
import com.simen.connectandroid.model.Device;
import com.simen.hellobytes.log.Log;
import com.simen.hellobytes.runtime.RuntimeHelper;

/**
 * Created on 2021/10/30 14:44.
 *
 * @author Simen
 */
public class DisconnectCommand extends AdbCommand<DisconnectCommand> {

    private static final String TAG = DisconnectCommand.class.getSimpleName();
    private Integer processCode;

    public DisconnectCommand(Device device) {
        super(device);
    }

    @Override
    public DisconnectCommand exec() {
        System.out.println("disconnect: " + device.getSimpleName());
        RuntimeHelper runtimeHelper = RuntimeHelper.create(CommandCreator
                .createAdb()
                .appendAdbDevice(device)
                .appendParam("disconnect", device.getAdbDeviceName())
                .build()).exec();

        processCode = runtimeHelper.getProcessCode();
        log = runtimeHelper.getOutputLog();

        Log.i(TAG, log);
        return this;
    }

    @Override
    public boolean execResult() {
        return log.contains("disconnected " + device.getAdbDeviceName()) || processCode == 0;
    }
}
